import axios, { AxiosResponse, InternalAxiosRequestConfig } from 'axios';
import { storeToRefs } from 'pinia';
import { useUserStore } from '@/store/module/user';

const baseURL =  `${import.meta.env.VITE_ENV === 'dev' ? '/api' : ''}${import.meta.env.VITE_BASE_API}`;

const service = axios.create({
  baseURL: baseURL,
  timeout: 6000 // 请求超时时间
});

service.defaults.headers.common['Content-Type'] = 'application/json;charset=UTF-8';
// request拦截器
service.interceptors.request.use(
  function (config: InternalAxiosRequestConfig) {
    return config;
  },
  function (error: unknown) {
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  function (response: AxiosResponse) {
    return Promise.resolve(response.data.data);
  },
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  function (error: any) {
    return Promise.reject(error);
  }
);

export default service;
